package com.ipartek.formacion.primerwebapp.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class login
 */
public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Cookie miAplicacion1 = null;
	Cookie miAplicacion2 = null;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			
				//PrintWriter out;
				//response.setContentType("text/html");
				//out = response.getWriter();
			
				//String title = "SALIDA DE DATOS";
				
				//out.println ("<H1> Hemos lleagdo con el Boton: "+ request.getParameter("opcion") + "</H1>");
				
		 String usuariosistema =  "admin";
		 String passsistema =  "admin";
		 //System.out.println (usuariosistema);
		//System.out.println (usuariosistema);
		//System.out.println (request.getParameter("opcion"));
		if (request.getParameter("opcion").equals("Opcion1")){
					
					/*out.println("</TITLE></HEAD><BODY>");
					out.println("<H1>" + title + "</H1>");			
					out.println ("<H1> El usuario es: "+ request.getParameter("uname") + "</H1>");
					out.println ("<H1> La Password es: "+ request.getParameter("pass") + "</H1>");
					
					out.println("</BODY></HTML>");
					out.close();*/
					/*response.sendRedirect("opcion1.jsp");*/
					//System.out.println (usuariosistema);
					//System.out.println (usuariosistema);
					if (request.getParameter("uname").equals(usuariosistema) && request.getParameter("pass").equals(passsistema)){
						
						if (request.getParameter("recordar")!= null && request.getParameter("recordar").equals("sirecordar")){
							Cookie miAplicacion1 = new Cookie("usuario", request.getParameter("uname"));
							Cookie miAplicacion2 = new Cookie("pass", request.getParameter("pass"));
							response.addCookie(miAplicacion1);
							response.addCookie(miAplicacion2);
						}
						else{
							if (miAplicacion1 != null && miAplicacion2 != null){
								miAplicacion1.setMaxAge(0);
								miAplicacion1.setPath("/");
								miAplicacion2.setMaxAge(0);
								miAplicacion2.setPath("/");
							}
						}
						
						System.out.println (request.getParameter("uname"));
						request.getSession().setAttribute("servletMsg1", request.getParameter("uname"));
						request.getSession().setAttribute("servletMsg2", request.getParameter("pass"));
						response.sendRedirect("opcion1.jsp");
					}else{
						request.getSession().setAttribute("servletMsg1", "No valido");
						request.getSession().setAttribute("servletMsg2", "No valido");
						response.sendRedirect("Login2.jsp");
					}
					//request.getSession().setAttribute("servletMsg", request.getParameter("uname"));
					//request.getSession().setAttribute("servletMsg", request.getParameter("pass"));
					//response.sendRedirect("opcion1.jsp");
				};
				if (request.getParameter("opcion").equals("Opcion2")){
					//out.println("</TITLE></HEAD><BODY>");
					//out.println("<H1>" + title + "</H1>");
					//out.println ("<H1> El usuario es: "+ request.getParameter("uname") + "</H1>");
					//out.println ("<H1> La Password es: "+ request.getParameter("pass") + "</H1>");
					
					//out.println("</BODY></HTML>");
					//out.close();
				};
			}

}
